import asyncio
from .alpha_evolve import AlphaEvolve
from .program_db import ProgramDB
from .llm_client import LLMClient
from .prompt_sampler import PromptSampler
from .evaluator import Evaluator
from .utils import load_program, save_program

async def main():
    """
    运行 NanoAlphaEvolve Agent 的主函数。
    此函数演示了如何设置和运行进化过程。
    """
    # 配置
    problem_path = "examples.simple_function"
    initial_program_file = f"{problem_path.replace('.', '/')}/initial_program.py"
    evaluation_module = f"{problem_path}.evaluate"
    population_size = 10
    num_iterations = 20

    # 初始化组件
    program_db = ProgramDB(population_size=population_size)
    llm_client = LLMClient()
    prompt_sampler = PromptSampler()
    evaluator = Evaluator(evaluation_module)

    # 加载初始程序
    initial_program = load_program(initial_program_file)
    initial_score = await evaluator.evaluate(initial_program)
    program_db.add_program(initial_program, initial_score)

    # 创建并运行 AlphaEvolve Agent
    alpha_evolve = AlphaEvolve(
        program_db=program_db,
        llm_client=llm_client,
        prompt_sampler=prompt_sampler,
        evaluator=evaluator,
        num_iterations=num_iterations,
        elite_selection_pressure=0.1
    )

    best_program = await alpha_evolve.run()

    # 保存最佳程序
    save_program(best_program, "best_program.py")
    print("\n最佳程序已保存至 best_program.py")

if __name__ == "__main__":
    asyncio.run(main())